clear all;
matlabpool(12);
%Setting Options
optionsLoose = optimset('TolX', 1e-5, 'TolFun',1e-5, 'MaxFunEvals', 1e5, 'MaxIter', 1e5);
optionsTight = optimset('TolX', 1e-8, 'TolFun',1e-8, 'MaxFunEvals', 1e8, 'MaxIter', 1e8);

UtilFuncParam = 1;
ChoiceProbForm = 1;
NumBootReps = 250;
RandomizationSeed = 12345;

%Loading Data
DATA = csvread('CCES_Matlab.csv', 1, 0);

VoteDem = DATA(:,1);
VoteDem_1 = DATA(:,1);
VoterIdeal = DATA(:,2);
RepPartyIdeal = DATA(:,3);
DemPartyIdeal = DATA(:,4);
RepCandIdeal = DATA(:,5);
DemCandIdeal = DATA(:,6);
IncRep = DATA(:,7);
IncDem = DATA(:,8);
ExpChallRep = DATA(:,9);
ExpChallDem = DATA(:,10);
InExpChallRep = DATA(:,11);
InExpChallDem = DATA(:,12);
Year2008 = DATA(:,13);
Year2010 = DATA(:,14);
Year2012 = DATA(:,15);
Age = DATA(:,16);
State_FE=DATA(:,17:65);
Dist_Index = DATA(:,67); 
Dist_FE = DATA(:,68:501);

CandTypeDem = horzcat(IncDem, ExpChallDem, InExpChallDem);
CandTypeRep = horzcat(IncRep, ExpChallRep, InExpChallRep);
clear DATA;


Lambdas = vertcat(0.5, 0.5, 0.5);
RHS = [Year2008, Year2010, Year2012];
Betas0 = vertcat(1, 1, 1, zeros(length(RHS(1,:)), 1)); Theta_0 = vertcat(Lambdas, Betas0, 1);

[thetaTightProbit1G, fvalProbit1G] = fminunc(@(Theta) IndivVoterObjFun(Theta,VoteDem, VoterIdeal, CandTypeDem, CandTypeRep, DemCandIdeal, DemPartyIdeal, RepCandIdeal, RepPartyIdeal, RHS, UtilFuncParam, ChoiceProbForm), Theta_0, optionsTight);
[SEsBoot1 thetaBoot1 exitflagBoot1] = getVarCovBootstrapCluster(thetaTightProbit1G, VoteDem, VoterIdeal, CandTypeDem, CandTypeRep, DemCandIdeal, DemPartyIdeal, RepCandIdeal, RepPartyIdeal, RHS, UtilFuncParam, ChoiceProbForm, NumBootReps, optionsLoose, optionsTight, RandomizationSeed, Dist_Index);

[IncAdv_Mat_Probit1] = getIncAdv(thetaTightProbit1G,VoteDem, VoterIdeal, CandTypeDem, CandTypeRep, DemCandIdeal, DemPartyIdeal, RepCandIdeal, RepPartyIdeal, RHS, UtilFuncParam, 1,Dist_Index);

for b = 1:NumBootReps
    [IncAdv_Mat_Probit1_Boot(:,b)] = getIncAdv(thetaBoot1(:,b) ,VoteDem, VoterIdeal, CandTypeDem, CandTypeRep, DemCandIdeal, DemPartyIdeal, RepCandIdeal, RepPartyIdeal, RHS, UtilFuncParam, 1,Dist_Index);
end

IncAdv_Mat_Probit1_SE = sqrt((1/(NumBootReps-1)).*sum((IncAdv_Mat_Probit1_Boot - repmat(mean(IncAdv_Mat_Probit1_Boot,2), 1, NumBootReps)).^2,2));

csvwrite('thetaTightProbit1.csv', thetaTightProbit1G);
csvwrite('fvalProbit1.csv', fvalProbit1G);
csvwrite('IncAdv_Mat_Probit1.csv', IncAdv_Mat_Probit1);
csvwrite('thetaBoot_Probit1.csv', thetaBoot1);
csvwrite('IncAdv_Mat_Probit1_Boot.csv', IncAdv_Mat_Probit1_Boot);
csvwrite('IncAdv_Mat_Probit1_SE.csv', IncAdv_Mat_Probit1_SE);


RHS = [Year2008, Year2010, Year2012, State_FE];
Betas0 = vertcat(1, 1, 1, zeros(length(RHS(1,:)), 1)); Theta_0 = vertcat(Lambdas, Betas0, 1);
[thetaTightProbit2G, fvalProbit2G] = fminunc(@(Theta) IndivVoterObjFun(Theta,VoteDem, VoterIdeal, CandTypeDem, CandTypeRep, DemCandIdeal, DemPartyIdeal, RepCandIdeal, RepPartyIdeal, RHS, UtilFuncParam, ChoiceProbForm), Theta_0, optionsTight);
[SEsBoot2 thetaBoot2 exitflagBoot2] = getVarCovBootstrapCluster(thetaTightProbit2G, VoteDem, VoterIdeal, CandTypeDem, CandTypeRep, DemCandIdeal, DemPartyIdeal, RepCandIdeal, RepPartyIdeal, RHS, UtilFuncParam, ChoiceProbForm, NumBootReps, optionsLoose, optionsTight, RandomizationSeed, Dist_Index);

[IncAdv_Mat_Probit2] = getIncAdv(thetaTightProbit2G,VoteDem, VoterIdeal, CandTypeDem, CandTypeRep, DemCandIdeal, DemPartyIdeal, RepCandIdeal, RepPartyIdeal, RHS, UtilFuncParam, 1,Dist_Index);

for b = 1:NumBootReps
    [IncAdv_Mat_Probit2_Boot(:,b)] = getIncAdv(thetaBoot2(:,b) ,VoteDem, VoterIdeal, CandTypeDem, CandTypeRep, DemCandIdeal, DemPartyIdeal, RepCandIdeal, RepPartyIdeal, RHS, UtilFuncParam, 1,Dist_Index);
end

IncAdv_Mat_Probit2_SE = sqrt((1/(NumBootReps-1)).*sum((IncAdv_Mat_Probit2_Boot - repmat(mean(IncAdv_Mat_Probit2_Boot,2), 1, NumBootReps)).^2,2));

csvwrite('thetaTightProbit2.csv', thetaTightProbit2G);
csvwrite('fvalProbit2.csv', fvalProbit2G);
csvwrite('IncAdv_Mat_Probit2.csv', IncAdv_Mat_Probit2);
csvwrite('thetaBoot_Probit2.csv', thetaBoot2);
csvwrite('IncAdv_Mat_Probit2_Boot.csv', IncAdv_Mat_Probit2_Boot);
csvwrite('IncAdv_Mat_Probit2_SE.csv', IncAdv_Mat_Probit2_SE);

%%

RHS = [Year2008, Year2010, Year2012, Dist_FE];

Betas0 = vertcat(1, 1, 1, zeros(length(RHS(1,:)), 1)); Theta_0 = vertcat(Lambdas, Betas0, 1);
[thetaTightProbit3G, fvalProbit3G] = fminunc(@(Theta) IndivVoterObjFun(Theta,VoteDem, VoterIdeal, CandTypeDem, CandTypeRep, DemCandIdeal, DemPartyIdeal, RepCandIdeal, RepPartyIdeal, RHS, UtilFuncParam, ChoiceProbForm), Theta_0, optionsTight);
[SEsBoot3 thetaBoot3 exitflagBoot3] = getVarCovBootstrapCluster(thetaTightProbit3G, VoteDem, VoterIdeal, CandTypeDem, CandTypeRep, DemCandIdeal, DemPartyIdeal, RepCandIdeal, RepPartyIdeal, RHS, UtilFuncParam, ChoiceProbForm, NumBootReps, optionsLoose, optionsTight, RandomizationSeed, Dist_Index);

[IncAdv_Mat_Probit3] = getIncAdv(thetaTightProbit3G,VoteDem, VoterIdeal, CandTypeDem, CandTypeRep, DemCandIdeal, DemPartyIdeal, RepCandIdeal, RepPartyIdeal, RHS, UtilFuncParam, 1,Dist_Index);

for b = 1:NumBootReps
    [IncAdv_Mat_Probit3_Boot(:,b)] = getIncAdv(thetaBoot3(:,b) ,VoteDem, VoterIdeal, CandTypeDem, CandTypeRep, DemCandIdeal, DemPartyIdeal, RepCandIdeal, RepPartyIdeal, RHS, UtilFuncParam, 1,Dist_Index);
end

IncAdv_Mat_Probit3_SE = sqrt((1/(NumBootReps-1)).*sum((IncAdv_Mat_Probit3_Boot - repmat(mean(IncAdv_Mat_Probit3_Boot,2), 1, NumBootReps)).^2,2));

csvwrite('thetaTightProbit3.csv', thetaTightProbit3G);
csvwrite('fvalProbit3.csv', fvalProbit3G);
csvwrite('IncAdv_Mat_Probit3.csv', IncAdv_Mat_Probit3);
csvwrite('thetaBoot_Probit3.csv', thetaBoot3);
csvwrite('IncAdv_Mat_Probit3_Boot.csv', IncAdv_Mat_Probit3_Boot);
csvwrite('IncAdv_Mat_Probit3_SE.csv', IncAdv_Mat_Probit3_SE);

%%

RandomizationSeed = 12345;

DATA = csvread('CCES_Matlab_IndivCovariates.csv', 1, 0);

VoteDem = DATA(:,1);
VoterIdeal = DATA(:,2);
RepPartyIdeal = DATA(:,3);
DemPartyIdeal = DATA(:,4);
RepCandIdeal = DATA(:,5);
DemCandIdeal = DATA(:,6);
IncRep = DATA(:,7);
IncDem = DATA(:,8);
ExpChallRep = DATA(:,9);
ExpChallDem = DATA(:,10);
InExpChallRep = DATA(:,11);
InExpChallDem = DATA(:,12);
Year2008 = DATA(:,13);
Year2010 = DATA(:,14);
Year2012 = DATA(:,15);
Age = DATA(:,16);
State_FE=DATA(:,17:65);
Dist_Index = DATA(:,67); 
Dist_FE = DATA(:,68:501);

HighSchool = DATA(:,503);
TwoYearCollege = DATA(:,504);
FourYearCollege = DATA(:,505);
PostBA = DATA(:,506);
Female = DATA(:,507);
Af_Am = DATA(:,508);
Latino = DATA(:,509);
Asian = DATA(:,510);
AttendRelServiceWeek = DATA(:,511);
Income = DATA(:,512);


CandTypeDem = horzcat(IncDem, ExpChallDem, InExpChallDem);
CandTypeRep = horzcat(IncRep, ExpChallRep, InExpChallRep);

%%
RHS = [Year2008, Year2010, Year2012, Dist_FE, HighSchool, TwoYearCollege, FourYearCollege, PostBA, Female, Af_Am, Latino, Asian, AttendRelServiceWeek, Income, Age];
%%

Betas0 = vertcat(1, 1, 1, zeros(length(RHS(1,:)), 1)); Theta_0 = vertcat(Lambdas, Betas0, 1);
[thetaTightProbit4G, fvalProbit4G] = fminunc(@(Theta) IndivVoterObjFun(Theta,VoteDem, VoterIdeal, CandTypeDem, CandTypeRep, DemCandIdeal, DemPartyIdeal, RepCandIdeal, RepPartyIdeal, RHS, UtilFuncParam, ChoiceProbForm), Theta_0, optionsTight);
%%
[SEsBoot4 thetaBoot4 exitflagBoot4] = getVarCovBootstrapCluster(thetaTightProbit4G, VoteDem, VoterIdeal, CandTypeDem, CandTypeRep, DemCandIdeal, DemPartyIdeal, RepCandIdeal, RepPartyIdeal, RHS, UtilFuncParam, ChoiceProbForm, NumBootReps, optionsLoose, optionsTight, RandomizationSeed, Dist_Index);

%%
[IncAdv_Mat_Probit4, IncAdvExpChallDem_Weighted_Holder4, IncAdvExpChallRep_Weighted_Holder4, IncAdvInExpChallDem_Weighted_Holder4, IncAdvInExpChallRep_Weighted_Holder4] = getIncAdv(thetaTightProbit4G,VoteDem, VoterIdeal, CandTypeDem, CandTypeRep, DemCandIdeal, DemPartyIdeal, RepCandIdeal, RepPartyIdeal, RHS, UtilFuncParam, 1,Dist_Index);
%%
for b = 1:NumBootReps
    [IncAdv_Mat_Probit4_Boot(:,b)] = getIncAdv(thetaBoot4(:,b) ,VoteDem, VoterIdeal, CandTypeDem, CandTypeRep, DemCandIdeal, DemPartyIdeal, RepCandIdeal, RepPartyIdeal, RHS, UtilFuncParam, 1,Dist_Index);
end

IncAdv_Mat_Probit4_SE = sqrt((1/(NumBootReps-1)).*sum((IncAdv_Mat_Probit4_Boot - repmat(mean(IncAdv_Mat_Probit4_Boot,2), 1, NumBootReps)).^2,2));
%%

csvwrite('thetaTightProbit4.csv', thetaTightProbit4G);
csvwrite('fvalProbit4.csv', fvalProbit4G);
csvwrite('IncAdv_Mat_Probit4.csv', IncAdv_Mat_Probit4);
csvwrite('thetaBoot_Probit4.csv', thetaBoot4);
csvwrite('IncAdv_Mat_Probit4_Boot.csv', IncAdv_Mat_Probit4_Boot);
csvwrite('IncAdv_Mat_Probit4_SE.csv', IncAdv_Mat_Probit4_SE);




%%

myfiguresize =  [100 1 10 8];
set(gcf, 'PaperPosition', myfiguresize);
fh = figure(1);
get(gca)
set(gcf, 'color', 'white'); 

hold 'on'
[f_Dem,xi_Dem] = ksdensity(IncAdvExpChallDem_Weighted_Holder4);
[f_Rep,xi_Rep] = ksdensity(IncAdvExpChallRep_Weighted_Holder4);
plot(xi_Dem, f_Dem, 'b--')
plot(xi_Rep, f_Rep, 'r')
legend('Democratic Inc Adv (Inc to Exp)', 'Republican Inc Adv (Inc to Exp)')
legend boxoff
title('Density of District Average Incumbency Advantage (House Elections)')
hold 'off'

saveas(fh, 'IncAdv_EC_Density', 'epsc'); 
close



myfiguresize =  [100 1 10 8];
set(gcf, 'PaperPosition', myfiguresize);
fh = figure(1);
get(gca)
set(gcf, 'color', 'white'); 

hold 'on'
[f_Dem,xi_Dem] = ksdensity(IncAdvInExpChallDem_Weighted_Holder4);
[f_Rep,xi_Rep] = ksdensity(IncAdvInExpChallRep_Weighted_Holder4);
hold 'on'
plot(xi_Dem, f_Dem, 'b-o')
plot(xi_Rep, f_Rep, 'r-*')
legend('Democratic Inc Adv (Inc to Inexp)', 'Republican Inc Adv (Inc to Inexp)')
legend boxoff
title('Density of District Average Incumbency Advantage (House Elections)')
hold 'off'


saveas(fh, 'IncAdv_IEC_Density', 'epsc'); 
close
%%

[h, p, ks2stat] = kstest2(IncAdvExpChallDem_Weighted_Holder4, IncAdvExpChallRep_Weighted_Holder4);
csvwrite('pValKSTest_ExpChall_House.csv', p)
[h, p, ks2stat] = kstest2(IncAdvInExpChallDem_Weighted_Holder4, IncAdvInExpChallRep_Weighted_Holder4);
csvwrite('pValKSTest_InExpChall_House.csv', p)

csvwrite('IncAdvExpChallDem_Weighted_Holder4.csv', IncAdvExpChallDem_Weighted_Holder4);
csvwrite('IncAdvExpChallRep_Weighted_Holder4.csv', IncAdvExpChallRep_Weighted_Holder4);
csvwrite('IncAdvInExpChallDem_Weighted_Holder4.csv', IncAdvInExpChallDem_Weighted_Holder4);
csvwrite('IncAdvInExpChallDem_Weighted_Holder4.csv', IncAdvInExpChallDem_Weighted_Holder4);


close



%%
Polarization_Factor_Vec= [0.5:0.1:2]';

for k = 1:length(Polarization_Factor_Vec)
    [IncDem_Weighted_Polarization(k,:), IncRep_Weighted_Polarization(k,:), IncAdvInExpChallDem_Weighted_Polarization(k,:), IncAdvInExpChallRep_Weighted_Polarization(k,:),  ExpChalltoIncDem_Weighted_Polarization(k,:), ExpChalltoIncRep_Weighted_Polarization(k,:), InExpChalltoIncDem_Weighted_Polarization(k,:), InExpChalltoIncRep_Weighted_Polarization(k,:)] = getPolarization(thetaTightProbit4G,VoteDem, VoterIdeal, CandTypeDem, CandTypeRep, DemCandIdeal, DemPartyIdeal, RepCandIdeal, RepPartyIdeal, RHS, UtilFuncParam, 1,Dist_Index, Polarization_Factor_Vec(k,:));
end


close

myfiguresize =  [100 1 10 8];
set(gcf, 'PaperPosition', myfiguresize);
fh = figure(1);
get(gca)
set(gcf, 'color', 'white');

hold 'on'
plot(Polarization_Factor_Vec, IncDem_Weighted_Polarization, '--bs', 'MarkerSize', 8, 'Marker', '+')
plot(Polarization_Factor_Vec, IncRep_Weighted_Polarization, '--rs', 'MarkerSize', 8)

plot(Polarization_Factor_Vec, IncAdvInExpChallDem_Weighted_Polarization, '--bs', 'MarkerSize', 8, 'Marker', '*')
plot(Polarization_Factor_Vec, IncAdvInExpChallRep_Weighted_Polarization, '--rs', 'MarkerSize', 8, 'Marker', 'o')

ylabel('Estimated Incumbency Advantage')
xlabel('Polarization Multiplier')
title('Simulated Incumbency Advantage as Function of Party Polarization (House Elections)')
legend('Democrats (Incumbents to Experienced Challengers)', 'Republicans (Incumbents to Experienced Challengers)', 'Democrats (Incumbents to Inexperienced Challengers)', 'Republicans (Incumbents to Inxperienced Challengers)')
legend('Location', 'NorthWest')
legend boxoff
hold 'off'

saveas(fh, 'Polarization', 'epsc'); 
csvwrite('IncDem_Weighted_Polarization_House.csv', IncDem_Weighted_Polarization);
csvwrite('IncRep_Weighted_Polarization_House.csv', IncRep_Weighted_Polarization);
csvwrite('IncAdvInExpChallDem_Weighted_Polarization_House.csv', IncAdvInExpChallDem_Weighted_Polarization);
csvwrite('IncAdvInExpChallRep_Weighted_Polarization_House.csv', IncAdvInExpChallRep_Weighted_Polarization);
close



Betas0 = vertcat(1, 1, 1, zeros(length(RHS(1,:)), 1)); Theta_0 = vertcat(vertcat(0, 0, 0), Betas0, 1);
[thetaTightProbit_InfoTest, fvalProbit_InfoTest] = fminunc(@(Theta) IndivVoterObjFunInfoTest(Theta,VoteDem, VoterIdeal, CandTypeDem, CandTypeRep, DemCandIdeal, DemPartyIdeal, RepCandIdeal, RepPartyIdeal, RHS, UtilFuncParam, ChoiceProbForm), Theta_0, optionsTight);

csvwrite('thetaTightProbit_InfoTest.csv', thetaTightProbit_InfoTest);
csvwrite('fvalProbit_InfoTest.csv', fvalProbit_InfoTest);


Betas0 = vertcat(1, 1, 1, zeros(length(RHS(1,:)), 1)); Theta_0 = vertcat(vertcat(0, 0, 0), Betas0, 0);
[thetaTightProbit_NoPolicy, fvalProbit_NoPolicy] = fminunc(@(Theta) IndivVoterObjFunNoPolicy(Theta,VoteDem, VoterIdeal, CandTypeDem, CandTypeRep, DemCandIdeal, DemPartyIdeal, RepCandIdeal, RepPartyIdeal, RHS, UtilFuncParam, ChoiceProbForm), Theta_0, optionsTight);

csvwrite('thetaTightProbit_NoPolicy.csv', thetaTightProbit_NoPolicy);
csvwrite('fvalProbit_NoPolicy.csv', fvalProbit_NoPolicy);

%% 
%%Creating Reciever Operator Chracteristic Graph

[PrProbit4] = PredictedProb(thetaTightProbit4G,VoteDem, VoterIdeal, CandTypeDem, CandTypeRep, DemCandIdeal, DemPartyIdeal, RepCandIdeal, RepPartyIdeal, RHS, UtilFuncParam, 1);
[FracIncorrectY0_Probit4, FracCorrectY1_Probit4]=get_ROC(PrProbit4, VoteDem);

thetaTightProbit_InfoTest = csvread('thetaTightProbit_InfoTest.csv');
[PrProbit_InfoTest] = PredictedProbInfoTest(thetaTightProbit_InfoTest,VoteDem, VoterIdeal, CandTypeDem, CandTypeRep, DemCandIdeal, DemPartyIdeal, RepCandIdeal, RepPartyIdeal, RHS, UtilFuncParam, 1);
[FracIncorrectY0_Probit_InfoTest, FracCorrectY1_Probit_InfoTest]=get_ROC(PrProbit_InfoTest, VoteDem);

thetaTightProbit_NoPolicy = csvread('thetaTightProbit_NoPolicy.csv');
[PrProbit_NoPolicy] = PredictedProbNoPolicy(thetaTightProbit_NoPolicy,VoteDem, VoterIdeal, CandTypeDem, CandTypeRep, DemCandIdeal, DemPartyIdeal, RepCandIdeal, RepPartyIdeal, RHS, UtilFuncParam, 1);
[FracIncorrectY0_Probit_NoPolicy, FracCorrectY1_Probit_NoPolicy]=get_ROC(PrProbit_NoPolicy, VoteDem);

myfiguresize =  [100 1 10 8];
set(gcf, 'PaperPosition', myfiguresize);
fh = figure(1);
get(gca)
set(gcf, 'color', 'white');




close
hold 'on'
line(FracCorrectY1_Probit4, FracIncorrectY0_Probit4)
line(FracCorrectY1_Probit_InfoTest, FracIncorrectY0_Probit_InfoTest, 'Marker', '+')
line(FracCorrectY1_Probit_NoPolicy, FracIncorrectY0_Probit_NoPolicy, 'Marker', 'o')
plot(0:0.05:1, 0:0.05:1, '--')
ylabel('True Positive Rate')
xlabel('False Positive Rate')
title('Reciever Operating Characteristics Curve')
legend('Full Model', 'Alternative Information Model', 'No Policy Model', 'No Predictive Power Model')
legend('Location', 'Southeast')
legend boxoff
hold 'off'

title('Reciever Operating Characteristics Curve')
legend('Full Model', 'Alternative Information Model', 'No Policy Model')
legend('Location', 'SouthEast')
legend boxoff
hold 'off'


legend('Full Model', 'Alternative Information Model', 'No Policy Model')
legend('Location', 'SouthEast')
legend boxoff
hold 'off'

cd('~/Research_Projects/Ideol_Sig_0612/Output')
saveas(fh, 'ROC_Graphs', 'epsc'); 
close



LogLikelihoodNoImpute = horzcat(-fvalProbit1G, -fvalProbit2G, -fvalProbit3G, -fvalProbit4G);

csvwrite('LogLikelihoodNoImputeCollect_Probit.csv', LogLikelihoodNoImpute);


labels = vertcat(char('Party Weight (Incumbent)','Party Weight (Exp. Chall)', 'Party Weight (Inexp. Chall)',  'Inexperienced Challenger', 'Experienced Challenger', 'Incumbent'));
blanks = char('');

FID = fopen('CCESTable1_Probit.tex', 'w');
fprintf(FID, '\\begin{table}[t]\\centering \n');
fprintf(FID,  '\\begin{tabular}{l*{4}{c}} \\hline\\hline \n');
fprintf(FID, '&\\multicolumn{1}{c}{(1)}& \\multicolumn{1}{c}{(2)}&\\multicolumn{1}{c}{(3)}&\\multicolumn{1}{c}{(4)}& \\\\ \n');
fprintf(FID, '&\\multicolumn{1}{c}{}&\\multicolumn{1}{c}{}&\\multicolumn{1}{c}{}&\\multicolumn{1}{c}{}&\\\\ \n');
fprintf(FID, '\\hline \n');
for k=1:4
     fprintf(FID, '%s & %8.3f & %8.3f & %8.3f & %8.3f \\\\ ', labels(k,:), round(thetaTightProbit1G(k)*10000)/10000, round(thetaTightProbit2G(k)*10000)/10000, round(thetaTightProbit3G(k)*10000)/10000, round(thetaTightProbit4G(k)*10000)/10000);
     fprintf(FID, '%s & %s & %s & %s & %s \\\\ \n', blanks, strcat('(', num2str(SEsBoot1(k)), ')'), strcat('(', num2str(SEsBoot2(k)), ')'), strcat('(', num2str(SEsBoot3(k)), ')'), strcat('(', num2str(SEsBoot4(k)), ')'));   
end
             fprintf(FID, '\\hline \n');
labels_IncAdv = vertcat(char('Dem Inc Adv (Exp Chall)',  'Rep Inc Adv (Exp Chall)', 'Dem Inc Adv (Inexp Chall)',  'Rep Inc Adv (Inexp Chall)', 'Dem Signaling (EC \%)', 'Rep Signaling (EC \%)', 'Dem Signaling (IEC \%)', 'Rep Signaling (IEC \%)'));
             
for k=1:4
     fprintf(FID, '%s & %8.3f & %8.3f & %8.3f & %8.3f \\\\ ', labels_IncAdv(k,:), round(IncAdv_Mat_Probit1(k)*10000)/10000, round(IncAdv_Mat_Probit2(k)*10000)/10000, round(IncAdv_Mat_Probit3(k)*10000)/10000, round(IncAdv_Mat_Probit4(k)*10000)/10000);
     fprintf(FID, '%s & %s & %s & %s \\\\ \n', blanks, strcat('(', num2str(IncAdv_Mat_Probit1_SE(k)), ')'), strcat('(', num2str(IncAdv_Mat_Probit2_SE(k)), ')'), strcat('(', num2str(IncAdv_Mat_Probit3_SE(k)), ')'), strcat('(', num2str(IncAdv_Mat_Probit4_SE(k)), ')'));   
end

             
for k=5:8
     fprintf(FID, '%s & %8.3f & %8.3f & %8.3f & %8.3f \\\\ ', labels_IncAdv(k,:), round(IncAdv_Mat_Probit1(k+4)*10000)/10000, round(IncAdv_Mat_Probit2(k+4)*10000)/10000, round(IncAdv_Mat_Probit3(k+4)*10000)/10000, round(IncAdv_Mat_Probit4(k+4)*10000)/10000);
     fprintf(FID, '%s & %s & %s & %s \\\\ \n', blanks, strcat('(', num2str(IncAdv_Mat_Probit1_SE(k+4)), ')'), strcat('(', num2str(IncAdv_Mat_Probit2_SE(k+4)), ')'), strcat('(', num2str(IncAdv_Mat_Probit3_SE(k+4)), ')'), strcat('(', num2str(IncAdv_Mat_Probit4_SE(k+4)), ')'));   
end

fprintf(FID, '\\hline \n');             
             

fprintf(FID, '%s & %8.0f & %8.0f & %8.0f & %8.0f \\\\ \n ', 'Observations', length(VoteDem_1), length(VoteDem_1), length(VoteDem_1), length(VoteDem));
fprintf(FID, '%s & %8.3f & %8.3f & %8.3f & %8.3f \\\\ \n ', 'Log-likelihood', -fvalProbit1G, -fvalProbit2G, -fvalProbit3G, -fvalProbit4G)
fprintf(FID, '\\hline\\hline \n');
fprintf(FID, '\\multicolumn{5}{l}{\\tiny Bootstrap standard errors in parentheses are clustered at the district level.}\\\\ \n');
fprintf(FID, '\\end{tabular}\n');
fprintf(FID, '\\end{table} \n');
fclose(FID);


matlabpool CLOSE